package Logic.main
;

import logic.regulardevices.Sound;
import logic.regulardevices.AlarmLamp;
import logic.regulardevices.WaterLeakageAlarm;
import logic.regulardevices.Stove;
import logic.regulardevices.TmpIn;
import logic.regulardevices.HeatIn;
import logic.regulardevices.SecurityAlarm;
import logic.regulardevices.IndoorLight;
import logic.regulardevices.Fan;
import logic.regulardevices.RegularDevice;
import logic.regulardevices.FireAlarm;
import logic.regulardevices.ElCon;
import logic.regulardevices.TmpRoof;
import logic.regulardevices.OutdoorLight;
import logic.regulardevices.Timer1;
import logic.regulardevices.TmpOut;
import logic.regulardevices.Timer2;
import logic.regulardevices.HeatRoof;
import java.net.UnknownServiceException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.IllegalFormatException;
import java.util.InputMismatchException;
import java.util.Scanner;



/**
 * The Virtual house takes commands from the house client
 * And sends responses back, as well as updating with measurements
 * from devices such as temperature sensors, power consumption etc.
 * 
 * The VirtualHouse also holds all devices, deciding what device should receive
 * what command, strips incoming commands and adds neccesary string-parts
 * to outgoing messages, such as device-name and '!'
 * @author Martin
 *
 */
public class VirtualHouse {

	private ArrayList<RegularDevice> devices;
	private ArrayList<RegularDevice> devicesBackup;
	private String default_Error = "error_";
	private Scanner sc;
	private boolean ecFlag = false;
	
	public VirtualHouse() {
		//addar ett exempel
		devices = new ArrayList<RegularDevice>();
		devices.add(new FireAlarm());
		devices.add(new TmpIn());
		devices.add(new TmpRoof());
		devices.add(new TmpOut());
		devices.add(new ElCon());
		devices.add(new Sound());
		devices.add(new WaterLeakageAlarm());
		devices.add(new Stove());
		devices.add(new HeatRoof());
		devices.add(new HeatIn());
		devices.add(new Fan());
		devices.add(new Timer1());
		devices.add(new Timer2());
		devices.add(new IndoorLight());
		devices.add(new OutdoorLight());
		devices.add(new AlarmLamp());
		devices.add(new SecurityAlarm());
	}
	
	/**
	 * Receives the command sent and responds with answers.
	 * @param cmd
	 * @return the response after command has been sent.
	 */
	public String receiveAndRespond(String cmd) {
		try {
			//Controls last char in string is a '!'
			if( cmd.charAt(cmd.length()-1) != '!' ) return default_Error+cmd+"_noExclamationMark"+'!';
			
			//Strips cmd of the !
			cmd= cmd.substring(0, cmd.length()-1);
			
			//if device is installed, send command.
			//first, chop down the command, send only the device-part.
			sc = new Scanner(cmd);
			sc.useDelimiter("[_]|[!]");
			
			String cmdDevice = null;
			if(sc.hasNext())
				cmdDevice = sc.next();
			else
				throw new InputMismatchException(); //Format is unsupported.

			/********************************
			 * FIRST CHECK IF COMMAND IS FOR THE HOUSE ITSELF, THEN GO TO DEVICES.
			 
			if(cmdDevice.equals("ec")) {
				if(sc.hasNext()) {
					String eccmd = sc.next();
					if(eccmd.equals("on")) {
						return electricityCutOn();
					}
					else if(eccmd.equals("off")){
						return electricityCutOff();
					}
					else if(eccmd.equals("check")) {
						
					}
				}
				else
					throw new InputMismatchException();
			}*/
			//********************************
			
			
			for(RegularDevice d : devices) {
				if(d.isRecipient(cmdDevice)) {
					if( sc.hasNext() )
						return d.receiveAndRespond(sc.next())+'!';
					else
						throw new InputMismatchException();
				}
			}
			//No device interpreting that command.
			return default_Error+cmd+"noDevice"+'!';
		} catch(StringIndexOutOfBoundsException e) {
			//If e.g. empty string sent
			return default_Error+"_StringIndexOutOfBoundsException!";
		} catch(InputMismatchException ie) {
			return default_Error+"_IllegalCommandFormat!";
		}
	}
	
	private String electricityCutOn() {
		if(!ecFlag) {
			ecFlag=true;
			devicesBackup = devices;
			devices = null;
		}
		return "ec_on";
	}
	private String electricityCutOff() {
		if(ecFlag) {
			ecFlag = false;
			devices=devicesBackup;
			devicesBackup=null; //clear the backup, or it will be overfilled next ec.
		}
		return "ec_off";
	}
}
